package com.currency.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.currency.exception.AppException;
import com.currency.pojo.WytCoinsTradeResumePojo;
import com.currency.request.NewTradeRequest;
import com.currency.request.UserIdNormalRequest;
import com.currency.request.WithdrawalOilCardRequest;
import com.currency.response.BaseResponse;
import com.currency.response.NewTradeResponse;
import com.currency.response.NormalResponse;
import com.currency.response.QueryTradeResponse;
import com.currency.service.TradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value = "TradeController", tags = {"交易信息接口"})
@RestController
@RequestMapping("/trade")
@Slf4j
public class TradeController {
    @Autowired
    private TradeService tradeService;

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    @ApiOperation("统一支付 / 提现 接口")
    @Transactional(rollbackFor = Exception.class)
    public NewTradeResponse newTrade(@RequestBody @Validated NewTradeRequest request) throws Exception {
        return tradeService.newTrade(request);
    }

    @GetMapping("/query")
    @ApiOperation(value = "查询交易结果接口，flag 参数为结果期望值后续取消，0-交易成功 1-交易取消")
    public QueryTradeResponse queryTrade(String tradeId, Integer flag) throws Exception {
        return tradeService.queryTrade(tradeId, flag);
    }

    @GetMapping("/cancel")
    @ApiOperation(value = "取消订单")
    public BaseResponse cancelTrade(String tradeId) throws Exception {
        return tradeService.cancelTrade(tradeId);
    }

    @GetMapping("/query-history")
    @ApiOperation(value = "查询所有订单交易记录")
    public NormalResponse queryHistory(String userId) {
        return tradeService.queryHistory(userId);
    }

    @GetMapping("query-pay-status")
    @ApiOperation(value = "根据状态查询订单记录,status 0-支付完成 1- 申请支付 2-支付中 3-已取消")
    public NormalResponse queryPayStatus(String userId, int status) {
        return tradeService.queryPayStatus(userId, status);
    }

    @PostMapping("withdrawal-oilCard")
    @ApiOperation(value = "提现到油卡")
    public BaseResponse withdrawalOilCard(@RequestBody WithdrawalOilCardRequest request) throws AppException {
        return tradeService.withdrawalOilCard(request);
    }


    @PostMapping("/query-resume")
    @ApiOperation(value = "查询金币交易记录概要")
    public NormalResponse<Page<WytCoinsTradeResumePojo>> queryTradeResume(@RequestBody UserIdNormalRequest request) {
        return tradeService.queryTradeResume(request);
    }
}
